package com.miplus.ccrm.uploadjob.controller;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.EnumRCode;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.uploadjob.controller.vo.LogSetReqVo;
import com.miplus.ccrm.uploadjob.service.JCheckTimer;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/srvmonitor")
public class monController {

    @Resource
    private JCheckTimer checkTimer;

    @RequestMapping(value = "/logger", method = RequestMethod.GET)
    public R<?> loggerList()
    {
        // 获取到 Logback 的 LoggerContext
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        // 获取系统所有的logger
        List<Map<String, String>> loggers = loggerContext.getLoggerList()
                .stream()
                // 日志记录器太多了， 为了演示这里进行了过滤。仅仅返回当前controller的logger
                .filter(logger -> logger.getName().startsWith("com.miplus.ccrm.uploadjob"))
                // 映射为map
                .map(logger -> {
                    Map<String, String> retVal = new LinkedHashMap<>();
                    // logger名称 & logger有效级别
                    retVal.put(logger.getName(), logger.getEffectiveLevel().levelStr);
                    return retVal;
                }).collect(Collectors.toList());
        return R.Success(loggers);
    }

    @RequestMapping(value = "/logset", method = RequestMethod.POST)
    public R logset(@RequestBody @Validated LogSetReqVo logSetReqVo) {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        // 获取logger
        Logger logger = loggerContext.exists(logSetReqVo.getLoggerName());
        if (logger == null) {
            return R.Error(EnumRCode.ArgErr,"日志记录器不存在：" + logSetReqVo.getLoggerName());
        }
        // 解析level
        Level newLevel = Level.toLevel(logSetReqVo.getLevel(), null);
        if (newLevel == null) {
            return R.Error(EnumRCode.ArgErr,"不是合法的日志级别：" + logSetReqVo.getLevel());
        }
        // 设置新的level
        logger.setLevel(newLevel);
        return R.Success();
    }

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public R<CmdResVo> hello()
    {
        if(checkTimer.isRunning){
            return R.Success(new CmdResVo(1,String.format("%d",checkTimer.getId())));
        }
        else{
            return R.Success(new CmdResVo(0));
        }
    }

    @RequestMapping(value = "/stopJob", method = RequestMethod.GET)
    public R stopJob()
    {
        checkTimer.StopTimer();
        return R.Success();
    }

    @RequestMapping(value = "/startJob", method = RequestMethod.GET)
    public R<CmdResVo> startJob()
    {
        if(!checkTimer.isRunning){
            checkTimer.InitTimer();
        }
        return R.Success(new CmdResVo(1,String.format("%d",checkTimer.getId())));
    }
}
